package com.daniel.cms.controller;

import com.daniel.cms.entity.AccountEntity;
import com.daniel.cms.service.AccountService;
import com.daniel.cms.utils.Mes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/4/21 10:38
 */
@Api(tags = "账号管理")
@RequestMapping("/user")
@RestController
public class AccountController {
	@Autowired
	AccountService service;

	@ApiOperation("用户登录")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "map", dataType = "object", value = "请求实体", required = true)})
	@PostMapping(value = "/login")
	public Mes login(@RequestBody Map<String, String> map) {
		return service.login(map);
	}

	@ApiOperation("获取用户信息")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "token", dataType = "string", value = "令牌", required = true)})
	@GetMapping(value = "/info")
	public Mes info(@RequestParam(name = "token") String token) {
		return service.info(token);
	}

	@ApiOperation("退出登录")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "token", dataType = "string", value = "token")})
	@PostMapping(value = "/logout")
	public Mes logout() {
		return Mes.success();
	}

	@ApiOperation("获取用户列表")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "page", dataType = "int", value = "页索引", required = true, defaultValue = "1"),
			@ApiImplicitParam(name = "limit", dataType = "int", value = "页大小", required = true, defaultValue = "10"),
			@ApiImplicitParam(name = "name", dataType = "string", value = "用户名", defaultValue = "")})
	@GetMapping("/list/{page}/{limit}")
	public Mes list(@PathVariable int page, @PathVariable int limit,
	                @RequestParam(name = "name", defaultValue = "") String name) {
		return service.list(page, limit, name);
	}

	@ApiOperation("新增用户")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "entity", dataType = "AccountEntity", value = "用户实体")})
	@PostMapping("/create")
	public Mes create(@Valid @RequestBody AccountEntity entity) {
		return service.create(entity);
	}

	@ApiOperation("修改用户")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "entity", dataType = "AccountEntity", value = "用户实体")})
	@PostMapping("/update")
	public Mes update(@RequestBody AccountEntity entity) {
		return service.update(entity);
	}

	@ApiOperation("删除用户")
	@ApiImplicitParams(value = {@ApiImplicitParam(name = "map", dataType = "object", value = "用户编号")})
	@PostMapping(value = "/delete")
	public Mes delete(@RequestBody Map<String, String> map) {
		return service.delete(map);
	}
}
